class Artist
  attr_reader :name, :genre
  
  def initialize(name='', genre='')
    @name, @genre = name, genre
    query unless name == ''
  end
  
  def query
    if GENRES.include?(@name)
      @genre = GENRES[@name]
      puts %{"#{@name}" found in local database, skipping last.fm query...}
    else
      puts %{Querying last.fm for: "#{@name}"...}
      begin
        doc = REXML::Document.new(Net::HTTP.get(URI("http://ws.audioscrobbler.com/1.0/artist/#{URI.escape(CGI.escape(@name))}/toptags.xml")))
      rescue EOFError => e
        puts "#{Color.error} Request timed out! Trying again..."
        query
      end
      genre = REXML::XPath.first(doc, "//toptags/tag/name")
      @genre = ((genre.nil?) ? '' : genre.text.strip)
      #@name = name.strip
    end
    
    GENRES[@name] = @genre unless GENRES.include?(@name)
  end
end
